/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.unipar.oficina.converters;

import br.com.unipar.oficina.modelo.cadastros.Colaborador;
import br.com.unipar.oficina.service.cadastros.ColaboradorService;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.springframework.web.jsf.FacesContextUtils;



/**
 *
 * @author Thiago Marques
 */

@FacesConverter(value = "converterColaborador")
public class ConverterColaborador implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value != null) {
            ColaboradorService colaboradorService = (ColaboradorService) FacesContextUtils.getWebApplicationContext(context).getBean("colaboradorService");
            return colaboradorService.getById(Long.valueOf(value));
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value instanceof Colaborador) {
            Colaborador colaborador = (Colaborador) value;
            return String.valueOf(colaborador.getIdColaborador());
        }
        return "";
    }
}

//@FacesConverter(value="converterColaborador")
//public class ConverterColaborador implements Converter {
//
//    @Override
//    public Object getAsObject(FacesContext context, UIComponent component, String value) {
//        if (value != null && !value.equals("")) {
//            ColaboradorService colaboradorService = (ColaboradorService) FacesContextUtils.getWebApplicationContext(context).getBean("colaboradorService");
//            return colaboradorService.getByIdColaborador(Long.valueOf(value));
//        }
//        return null;
//    }
//
//    @Override
//    public String getAsString(FacesContext context, UIComponent component, Object value) {
//        if (value instanceof Colaborador) {
//            Colaborador colaborador = (Colaborador) value;
//            return String.valueOf(colaborador.getIdColaborador());
//        }
//        return "";
//    }
//
//}
